#include "BarTest.h"
#include <QRadialGradient>
BarTest::BarTest(QWidget *parent) : QWidget(parent)
{
    setStyleSheet("background-color:red;");
}

void BarTest::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
     painter.setRenderHint(QPainter::Antialiasing, true);

     int radius = 20;
     int arcHeight = 4;

     // >> 1（右移1位）相当于width() / 2
     painter.translate(width() >> 1, height() >> 1);

     /**
      * 参数二：半径
      * 参数三：开始的角度
      * 参数四：指扫取的角度-顺时针（360度 / 8 = 45度）
      * 参数五：圆环的高度
      * 参数六：填充色
     **/
     gradientArc(&painter, radius, 0,  250, arcHeight, qRgb(200, 200, 0));
//     gradientArc(&painter, radius, 45, 45, arcHeight, qRgb(200, 0, 200));
//     gradientArc(&painter, radius, 90, 45, arcHeight, qRgb(0, 200, 200));
//     gradientArc(&painter, radius, 135, 45, arcHeight, qRgb(200, 0, 0));
//     gradientArc(&painter, radius, 225, 45, arcHeight, qRgb(0, 200, 0));
//     gradientArc(&painter, radius, 180, 45, arcHeight, qRgb(0, 0, 200));
//     gradientArc(&painter, radius, 270, 45, arcHeight, qRgb(0, 0, 0));
//     gradientArc(&painter, radius, 315, 45, arcHeight, qRgb(150, 150, 150));
}

void BarTest::gradientArc(QPainter *painter, int radius, int startAngle, int angleLength, int arcHeight, QRgb color)
{
//    // 渐变色
//    QRadialGradient gradient(0, 0, radius);
//    gradient.setColorAt(0, Qt::white);
//    gradient.setColorAt(1.0, color);
//    painter->setBrush(gradient);

//    // << 1（左移1位）相当于radius*2 即：150*2=300
//    //QRectF(-150, -150, 300, 300)
//    QRectF rect(-radius, -radius, radius << 1, radius << 1);
//    QPainterPath path;
//    path.arcTo(rect, startAngle, angleLength);

//    painter->setPen(Qt::NoPen);
//    painter->drawPath(path);

    // 渐变色
     QRadialGradient gradient(0, 0, radius);
     gradient.setColorAt(0, Qt::white);
     gradient.setColorAt(1.0, color);
     painter->setBrush(gradient);

     // << 1（左移1位）相当于radius*2 即：150*2=300
     //QRectF(-150, -150, 300, 300)
     QRectF rect(-radius, -radius, radius << 1, radius << 1);
     QPainterPath path;
     path.arcTo(rect, startAngle, angleLength);

     // QRectF(-120, -120, 240, 240)
     QPainterPath subPath;
     subPath.addEllipse(rect.adjusted(arcHeight, arcHeight, -arcHeight, -arcHeight));

     // path为扇形 subPath为椭圆
     path -= subPath;

     painter->setPen(Qt::NoPen);
     painter->drawPath(path);

}

